package com.etime.shycourse.controller.salers;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.pojo.Department;
import com.etime.shycourse.services.salers.interfaces.DepartmentServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 销售团队管理 前端控制器
 * </p>
 *
 * @author wb
 * @since 2022-03-24
 */
@Api(tags = "后台-销售团队管理")
@RestController
@RequestMapping("/sys/department")
public class DepartmentController {
    @Autowired
    private DepartmentServices service;

    @ApiOperation("获取全部,可以用的市场部门 ")
    @GetMapping("/get")
    public ListMessage<Department> selectAllList() {
        ListMessage<Department> msg = new ListMessage<> ();
        List<Department> data = service.selectAllList ();
        if (data.size () > 0) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到可以用市场部门");
        }
        return msg;
    }

    @ApiOperation("创建销售团队。返回int 成功 =1 失败=0 ")
    @PostMapping("/inset")
    public BaseMessage insertDepartment(Department department) {
        BaseMessage msg = new BaseMessage ();
        int i = service.insertDepartment (department);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("创建成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("创建失敗");
        }
        return msg;
    }

    @ApiOperation("修改销售团队。返回int 成功 =1 失败=0 ")
    @PostMapping("/up")
    public BaseMessage updateDepartment(Department department) {
        BaseMessage msg = new BaseMessage ();
        int i = service.updateDepartment (department);
        if (i > 0) {
            msg.setCode (i);
            msg.setMsg ("修改成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改失敗");
        }
        return msg;
    }

    @ApiOperation("根据id查询 销售团队 ")
    @GetMapping("/get/{Id}")
    public PojoMessage selectDepartmentById(@PathVariable Integer Id) {
        PojoMessage msg = new PojoMessage ();
        Department data = service.selectDepartmentById (Id);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有数据");
        }
        return msg;
    }

    @ApiOperation("销售团队管理 分頁")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
    })
    @GetMapping("/page")
    public PageMessage<Department> selectPage(Long pagenum, Long pagesize) {
        PageMessage<Department> msg = new PageMessage<> ();
        Page<Department> data = service.selectPage (pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }
}

